package kr.ac.skhu.m.controller.etc;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.skhu.m.command.CommentCmd;
import kr.ac.skhu.m.domain.Comment;
import kr.ac.skhu.m.service.CommentS;
import kr.ac.skhu.m.util.PaggingUtil;
import kr.ac.skhu.m.util.UUID;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class CommentC extends AbstractController {
	
	private int mode;
	private String successView;		
	private CommentS commentS;
	
	public void setMode(int mode) {
		this.mode = mode;
	}
	public void setSuccessView(String successView) {
		this.successView = successView;
	}
	public void setCommentS(CommentS commentS) {
		this.commentS = commentS;
	}
	
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = null;
		
		switch (mode) {
			case 1:
				mav = write(request, response);
				break;
			case 2:
				mav = delete(request, response);
				break;
		}
		
		return mav;
	}
	
	private ModelAndView write(HttpServletRequest request,
			HttpServletResponse response){
		
		ModelAndView mav = null;
		int result = 0;
		int errorCode = 0;
		String articleNo = ServletRequestUtils.getStringParameter(request, "articleNo", "");
		int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 1);
		String writer = ServletRequestUtils.getStringParameter(request, "writer", "");
		String password = ServletRequestUtils.getStringParameter(request, "password", "");
		String comment = ServletRequestUtils.getStringParameter(request, "comment", "");
		String commentNo = UUID.getUUID();
		
		/**
		 * 이름 유효성 검사
		 */
		if(writer.equals("")){
			errorCode = 11;
		}else if(writer.length() < 2 || 8 < writer.length()){
			errorCode = 12;
		}
		
		/**
		 * 비밀번호 유효성 검사
		 */
		else if(password.equals("")){
			errorCode = 21;
		}else if(password.length() < 4 || 12 < password.length()){
			errorCode = 22;
		}
		
		/**
		 * 내용 유효성 검사
		 */
		else if(comment.equals("")){
			errorCode = 31;
		}
		
		if(errorCode == 0){
			System.out.println("insert");
			Comment commentObj = new Comment();
			commentObj.setArticleNo(articleNo);
			commentObj.setComment(comment);
			commentObj.setCommentNo(commentNo);
			commentObj.setPassword(password);
			commentObj.setWriter(writer);
			result = commentS.insertComment(commentObj);
		}
		
		mav = new ModelAndView("redirect:/etc/used_view.do?pageNum=" + pageNum + "&articleNo=" + articleNo + "&ec=" + errorCode);
		
		return mav;
	}
	
	private ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response){
		
		ModelAndView mav = new ModelAndView(successView);
	
		return mav;
	}
	
}
